package com.zty.schedule.api;

import com.zty.schedule.entity.JsonResult;
import com.zty.schedule.entity.PlanZ;
import com.zty.schedule.service.PlanService;
import com.zty.schedule.service.PlanZService;
import com.zty.schedule.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Api(description = "进度接口")
@RestController
@RequestMapping("PlanZ")
@CrossOrigin
public class PlanZController {
    @Autowired
    private PlanService planService;

    @Autowired
    private PlanZService planZService;

    @ApiOperation(value = "增加项目进度信息", notes = "")
    @PostMapping("/InsertPlanZ")
    public JsonResult InsertPlanZ(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int Days = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            long time = sdf.parse((String) map.get("start_time")).getTime();
            long time1 = sdf.parse((String) map.get("end_time")).getTime();
            Days = (int) ((time1 -time) /(24 * 60 * 60 * 1000));//计算计划天数
            map.put("days",Days+1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int i = planZService.InsertPlanZ(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("增加成功!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("增加失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除项目进度信息",notes = "")
    @PostMapping("/DeletePlanZ")
    public JsonResult DeletePlanZ(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = planZService.DeletePlanZ(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("删除成功!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改项目进度信息",notes = "")
    @PostMapping("/UpdatePlanZ")
    public JsonResult UpdatePlanZ(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = planZService.UpdatePlanZ(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("修改成功!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页查询项目进度信息",notes = "")
    @PostMapping("/FindPlanZ")
    public Page FindPlanZ(@RequestBody Map map){
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        long i=1;
        page.setTotal(i);
        page.setItems(planZService.FindPlanZ(map));
        return page;
    }



}
